import { webSocketStore, userStore } from "~/store";
const handleChannelAction = (device_sn, cmd, additionalPayload = {}) => {
    const user_id = userStore.me?.user_id || '';
    const params = {
        action: 'publish',
        topic: device_sn,
        payload: JSON.stringify({
            cmd,
            user_id: `app/${user_id}`,
            ...additionalPayload
        }),
    };
    webSocketStore.sendMessage(JSON.stringify(params));
}


// 获取设备是否在线
export const getDeviceStatus = (device_sn) => {
    const user_id = userStore.me?.user_id || '';
    const params = {
        action: 'online_status',
        payload: JSON.stringify({
            sn: device_sn,
            user_id: `app/${user_id}`,
        }),
    };
    webSocketStore.sendMessage(JSON.stringify(params));
}

// 退出房间
export const exitChannel = (device_sn) => handleChannelAction(device_sn, 'exit_rtc');

// 加入房间
export const joinCallChannel = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'start_rtc', additionalPayload);

// 查询设备版本号
export const getDeviceVersion = (device_sn) => handleChannelAction(device_sn, 'version');

// 开始升级
export const startUpdate = (device_sn) => handleChannelAction(device_sn, 'update');

// 查询设备音量
export const getVolume = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'volume', additionalPayload);

// 设置设备音量
export const setVolume = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'volume', additionalPayload);

// 查询接听权限
export const getCallRole = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'auto_answer', additionalPayload);

// 设置接听权限
export const setCallRole = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'auto_answer', additionalPayload);

// 查询一氧化碳报警
export const getDeviceCoInterval = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'co_interval', additionalPayload);

// 设置一氧化碳报警
export const setDeviceCoInterval = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'co_interval', additionalPayload);

// 启动睡眠袋
export const startSleep = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'sleep_data_set', additionalPayload);

// 查询是否有硬件插入监测 睡眠袋、手指夹
export const getIsConnectDevice = (device_sn) => handleChannelAction(device_sn, 'typeC_connected_device_request');

// 手指夹实时测量
export const startFinger = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'fingerClip_user_sample', additionalPayload);

// yy01手动连接子设备
export const linkSubDevice = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'ble_link', additionalPayload);

// 发送对应蓝牙远程协议
export const sendBleSocket = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'ble_ctrl', additionalPayload);

// 重启设备
export const rebootDevice = (device_sn) => handleChannelAction(device_sn, 'reboot',);

export const getBleRssi = (device_sn, additionalPayload) => handleChannelAction(device_sn, 'ble_sn', additionalPayload);
